package com.qfedu.service.impl;

import com.qfedu.dao.AccountDao;
import com.qfedu.pojo.Account;
import com.qfedu.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;


//    public void setAccountDao(AccountDao accountDao) {
//        this.accountDao = accountDao;
//    }

//    private TransactionTemplate transactionTemplate;
//
//    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
//        this.transactionTemplate = transactionTemplate;
//    }
    /*  private TransactionManager transactionManager;

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }*/

    //转账业务

    public void transfer(String sourceName, String targetName, double money) {

        Account sourceAccount = accountDao.findAccountByName(sourceName);
        //根据名称查询转入账用户
        Account targetAccount = accountDao.findAccountByName(targetName);
        //转出账户扣钱
        sourceAccount.setMoney(sourceAccount.getMoney() - money);
        //转入账户加钱
        targetAccount.setMoney(targetAccount.getMoney() + money);
        //更新转出账户
        accountDao.updateAccount(sourceAccount);
//        int i = 1/0;
        //更新转入账户
        accountDao.updateAccount(targetAccount);
    }
}
